package com.ctyon.watchmanager.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ctyon.common.util.TSnackbarUtil;
import com.ctyon.netlibrary.net.revert.BaseResponseEntity;
import com.ctyon.netlibrary.use.BaseObserver;
import com.ctyon.watchmanager.BasicApp;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.databinding.FragmentFlowerBinding;
import com.ctyon.watchmanager.model.net.service.NetService;
import com.ctyon.watchmanager.present.FlowerPresenter;
import com.ctyon.watchmanager.ui.BaseFragment;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * CreateDate：19-6-25 on 上午9:14
 * Describe:
 * Coder: lee
 */
public class FlowerFragment extends BaseFragment {

    public static final String TAG = "FlowerFragment";

    private FragmentFlowerBinding mBinding;

    public static FlowerFragment newInstance() {
        final FlowerFragment fragment = new FlowerFragment();
        final Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_flower, container, false);
        mBinding.setFlowerPresenter(flowerPresenter);

        return mBinding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mBinding.setCount(1);
        mBinding.setAddAble(true);
    }

    private FlowerPresenter flowerPresenter = new FlowerPresenter() {
        @Override
        public void addFlowerClick() {
            mBinding.setCount(mBinding.getCount() + 1);
            if (mBinding.getCount() == 9){
                mBinding.setAddAble(false);
            }
            if (!mBinding.getMinusAble()){
                mBinding.setMinusAble(true);
            }
        }

        @Override
        public void minusFlowerClick() {
            mBinding.setCount(mBinding.getCount() - 1);
            if (mBinding.getCount() == 1){
                mBinding.setMinusAble(false);
            }
            if (!mBinding.getAddAble()){
                mBinding.setAddAble(true);
            }
        }

        @Override
        public void onSendFlowerClick(int count) {
            final String method = "flower";
            if (NetService.getInstance().isDisable(method)) {
                TSnackbarUtil.TSBError(getActivity(), "请稍候...");
                return;
            }
            NetService.getInstance()
                    .flower(BasicApp.getInstance().getManager().getManagerId(), BasicApp.getInstance().getWatch().getMeid(), count)
                    .timeout(10, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<BaseResponseEntity>() {
                        @Override
                        public void onSuccess(BaseResponseEntity responseEntity) {
                            Logger.d("onSuccess : " + responseEntity.toString());
                            TSnackbarUtil.TSBSuccess(getActivity(), "操作成功");
                        }

                        @Override
                        public String onOver() {
                            return method;
                        }
                    });
        }
    };

}
